<?php

// +----------------------------------------------------------------------
// |ZBPHP[基于ThinkPHP5.1开发]
// +----------------------------------------------------------------------
// | Copyright (c) 2016-2019 http://www.benbenwangluo.com
// +----------------------------------------------------------------------
// | Author 似水星辰 [ 2630481389@qq.com ]
// +----------------------------------------------------------------------
// | 中犇软件 技术六部 出品
// +----------------------------------------------------------------------
namespace app\order\admin;

use app\admin\admin\Base;
use service\Format;
use app\order\model\OrderInvoice;
use app\common\model\Order as OrderModel;
/**
 * 发票管理
 * @author 晓风<215628355@qq.com>
 * @date 2021-11-6 17:11:12
 */
class Invoice extends Base {

   
    
    /**
     * 发票列表
     * @return mixed
     * @author jxy [415782189@qq.com]
     */
    public function invoice_list()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        // 查询
        $order_sn = input('param.order_sn');
        $status = input('param.invoice_status', '');
        $create_time = input('param.invoice_add_time');
        $invoice_send_goods_num = input('param.invoice_send_goods_num');
        $invoice_title = input('param.invoice_title');
        $map = [];
        $map1 = [];
        if ($order_sn) {
            $map['oi.order_sn'] = $order_sn;
        }
        if ($create_time) {
            $map1[] = ['oi.invoice_add_time', '>=', ($create_time . ' 00:00:00')];
            $map1[] = ['oi.invoice_add_time', '<=', ($create_time . ' 23:59:59')];
        }
        if (!empty($invoice_send_goods_num)) {
            $map['oi.invoice_send_goods_num'] = $invoice_send_goods_num;
        }
        if (!empty($invoice_title)) {
            $map['oi.invoice_title'] = $invoice_title;
        }
        if ($status !== '') {
            $map['oi.invoice_status'] = $status;
            $this->assign('active', $status);
            $this->assign('field', 'invoice_status');
        }

        // 数据列表
        $data_list = OrderInvoice::alias('oi')
            ->field("o.real_money,o.aid,oi.*")
            ->where($map)
            ->where($map1)
            ->join('order o', 'o.order_sn = oi.order_sn', 'LEFT')
            ->order('oi.invoice_add_time DESC')
            ->paginate(15, false, ['query' => request()->param()])
            ->each(function ($item) {
                $item['invoice_type_name'] = ['1' => lang('个人发票'), '2' => lang('公司发票')][$item['invoice_type']];
                $item['invoice_status_name'] = ['1' => lang('申请开票中'), '2' => lang('已开票')][$item['invoice_status']];
                $item['invoice_add_time'] = date('Y-m-d H:i:s', $item['invoice_add_time']);
                return $item;
            });

        // tab栏
        $tab[] = ['title' => lang('申请开票中'), 'url' => url('order/order/invoice_list', 'invoice_status=1'), 'value' => 1, 'field' => 'invoice_status'];
        $tab[] = ['title' => lang('已开票'), 'url' => url('order/order/invoice_list', 'invoice_status=2'), 'value' => 2, 'field' => 'invoice_status'];

        $this->assign('tab_list', $tab);
        $this->assign('list', $data_list);
        $this->assign('pages', $data_list->render());

        return $this->fetch();
    }

    /**
     * 开票
     */
    public function invoice()
    {
        // 获取参数值
        $id = input('param.id');
        $order_sn = input('param.order_sn');
        $order_status = OrderModel::where(['order_sn' => $order_sn])->value('status');
        $this->assign('id', $id);
        $this->assign('order_sn', $order_sn);
        $this->assign('order_status', $order_status);
        return $this->fetch();
    }
    
    
    /**
     * 邮寄发票
     */
    public function invoiceUpdate()
    {
        // 获取参数值
        $invoice_express_no = input('param.invoice_express_no');
        $order_sn = input('param.order_sn');
        $express_company_id = input('param.express_company_id');
        // 参数校验
        if (empty($invoice_express_no) || empty($order_sn)) {
            return json(['status' => '5000', 'msg' => lang('参数错误')]);
        }

        // 启动事务
        Db::startTrans();
        try {
            // 修改发票发货状态
            OrderInvoice::where(['order_sn' => $order_sn])->update(['invoice_send_goods_num' => $invoice_express_no, 'invoice_status' => 2, 'invoice_update_time' => time(), 'billing_type' => 2, 'express_company_id' => $express_company_id]);
            OrderModel::where(['order_sn' => $order_sn])->update(['invoice_status' => 2]);

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return json(['status' => '5000', 'msg' => lang('邮寄修改失败')]);
        }

        return json(['status' => '2000', 'msg' => lang('修改成功')]);
    }

    /**
     * 上传电子发票
     */
    public function invoice_edit()
    {
        $id = $order_sn = input('param.id');
        if ($id === null) {
            $this->error(lang('参数错误'));
        }
        // 保存数据
        if ($this->request->isPost()) {
            $data['invoice_img'] = request()->post('invoice_img');
            if (!$data['invoice_img']) {
                $this->error(lang('请上传发票'));
            }
            $data['invoice_status'] = 2;
            $data['invoice_update_time'] = time();
            $DocumentModel = new OrderInvoice();
            $result = $DocumentModel->where(['id' => $id])->update($data);
            if (false === $result) {
                $this->error($DocumentModel->getError());
            }
            $this->success(lang('编辑成功'), 'index');
        }
        // 获取数据
        $info = OrderInvoice::get($id);
        $fields = [
            ['type' => 'image', 'name' => 'invoice_img', 'title' => lang('电子发票'), 'extra' => $columns],
        ];
        $this->assign('page_title', lang('上传电子发票'));
        $this->assign('form_items', $this->setData($fields, $info));
        return $this->fetch('admin@public/edit');
    }

}
